package com.ls.controller;

import com.ls.model.User;
import com.sun.org.glassfish.gmbal.ParameterNames;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class HelloController {

    @RequestMapping("/index")
    public String index() {
        return "index";
    }

    @RequestMapping(value = "/target"
            , method = {RequestMethod.POST, RequestMethod.GET}
            , params = {"username"})
    public String target(String username) {
        System.out.println(username);
        return "target";
    }

    @PostMapping(value = "/pm")
    public String pm() {
        return "pm";
    }

    @GetMapping(value = "/testRest/{id}/{username}")
    public String testRest(@PathVariable("id") String id, @PathVariable("username") String username) {
        System.out.println(id);
        System.out.println(username);
        return "target";
    }

    @RequestMapping(value = "/testRequest")
    public String testRequest(HttpServletRequest request) {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        request.setAttribute("rrr", "request123");
        session.setAttribute("sss", "session123");
        application.setAttribute("aaa", "application123");
        System.out.println(request.getParameter("username"));

        return "target";
    }

    @RequestMapping(value = "/testParam")
    public String testParam(String username
            , @RequestParam("pass_word") String password
            , @RequestHeader("Host") String host
            , @CookieValue("JSESSIONID") String cookie) {

        System.out.println(username);
        System.out.println(password);
        System.out.println(host);
        System.out.println(cookie);

        return "target";
    }

    @RequestMapping(value = "/testPOJO")
    public String testPOJO(User user) {

        System.out.println(user);
        return "target";
    }

    @RequestMapping(value = "/testModelAndView")
    public ModelAndView testModelAndView(Model model) {

        ModelAndView mav = new ModelAndView();

        mav.addObject("testmav", "hellomav");
        mav.setViewName("target");
        return mav;
    }

}
